# -*- coding: iso-8859-1 -*-
# file Space.py
# author Rocco Martino
#
############################################################################
#    Copyright (C) 2015 by Rocco Martino                                   #
#    martinorocco@gmail.com                                                #
#                                                                          #
#    This program is free software; you can redistribute it and/or modify  #
#    it under the terms of the GNU Lesser General Public License as        #
#    published by the Free Software Foundation; either version 2.1 of the  #
#    License, or (at your option) any later version.                       #
#                                                                          #
#    This program is distributed in the hope that it will be useful,       #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#    GNU Lesser General Public License for more details.                   #
#                                                                          #
#    You should have received a copy of the GNU Lesser General Public      #
#    License along with this program; if not, write to the                 #
#    Free Software Foundation, Inc.,                                       #
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
############################################################################

############################################################################
from . import ODEObject, DefaultNearCallback, Joint
############################################################################




############################################################################
# ........................................................................ #
# ........................................................................ #
############################################################################




############################################################################
# ........................................................................ #
class Space(ODEObject.ODEObject):





############################################################################
############################################################################









############################################################################
    def __init__(self, obj, data):
        super(Space, self).__init__(obj, data)

        self.ClassName = "osgODE::Space"
        self.CheckUniqueID = False
############################################################################




############################################################################
    def writeData(self, writer):
        super(Space, self).writeData(writer)


        world = self.Data.CurrentScene.world


        gravity = self.Data.CurrentScene.game_settings.physics_gravity * -1.0





        try:
            erp = world["oo_erp"]
        except:
            erp = 0.2





        try:
            cfm = world["oo_cfm"]
        except:
            cfm = 1.0e-5





        try:
            contact_surface_layer = world["oo_contact_surface_layer"]
        except:
            contact_surface_layer = 0.005





        try:
            if world["oo_world_step"] > 0 :
                world_step = "dWorldStep"
            else:
                world_step = "dWorldQuickStep"
        except:
            world_step = "dWorldQuickStep"




        writer.writeLine("Gravity 0 0 %f" % gravity)

        writer.writeLine("ERP %f" % erp)
        writer.writeLine("CFM %f" % cfm)

        writer.writeLine("ContactSurfaceLayer %f" % contact_surface_layer)

        writer.writeLine("WorldStepFunction %s" %world_step)




        object_list = self.createObjectList()

        num_objects = len(object_list)

        writer.moveIn("ObjectList %d" % num_objects)

        for obj in object_list:
            obj.writeObject(writer)

        writer.moveOut("ObjectList %d" % num_objects)


        #if self.Data.ExportGame:
        if True:
            writer.moveIn("FrontEventsBuffer TRUE")
            writer.moveIn("osgODE::Events")
            writer.writeLine("UniqueID %d" % self.Data.UniqueID.generate() )
            writer.moveOut("osgODE::Events")
            writer.moveOut("FrontEventsBuffer TRUE")

            writer.moveIn("BackEventsBuffer TRUE")
            writer.moveIn("osgODE::Events")
            writer.writeLine("UniqueID %d" % self.Data.UniqueID.generate() )
            writer.moveOut("osgODE::Events")
            writer.moveOut("BackEventsBuffer TRUE")




        near_cbk = DefaultNearCallback.DefaultNearCallback(None, self.Data)

        writer.moveIn("NearCallback TRUE")

        near_cbk.writeObject(writer)

        writer.moveOut("NearCallback TRUE")
############################################################################




############################################################################
    def createObjectList(self):

        object_list = []


        for obj in self.Data.CurrentScene.objects:

            if obj.parent or not obj.is_visible(self.Data.CurrentScene):
                continue

            ode_obj = ODEObject.createObject(obj, self.Data)

            if ode_obj:

                object_list.append(ode_obj)

                for constraint in obj.constraints:

                    c = Joint.create(obj, self.Data, constraint)

                    if c:
                        object_list.append( c )




        return object_list
############################################################################




# ........................................................................ #
############################################################################
